/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Mobilottery.UI.Forms;

import Mobilottery.AppManager;
import Mobilottery.Constants.Consts;
import Mobilottery.Constants.Consts.Screens;
import Mobilottery.Controls.NumericTable;
import Mobilottery.Interfaces.IAlertCommandListener;
import Mobilottery.Interfaces.IBackgroundOperation;
import Mobilottery.Utils.UtilsConnection;
import Mobilottery.XmlManager;
import Mobilottery.XmlManager.OpenGamesDetails;
import Mobilottery.XmlManager.ParticipateInGameDetails;
import javax.microedition.lcdui.*;

/**
 *
 * @author WindowsVista
 */
public class FormNewGame2 extends Form implements CommandListener
{

    NumericTable _nt;
    OpenGamesDetails _openGamesDetails;
    int _selectedGameIndex;

    public FormNewGame2(Object p_obj)
    {
        super("Selecet 6 numbers:");

        super.setTitle("Selecet 6 numbers:");

        int rows = 8;
        int cols = 5;
        int w = this.getWidth();
        int h = this.getHeight();
        int dx = (int) (w * 0.9) / cols;
        int dy = (int) (h * 0.9) / rows;
        _nt = new NumericTable(null, rows, cols, dx, dy);
        _nt.setLayout(Item.LAYOUT_CENTER);
        this.append(_nt);

        Object[] arrObj = (Object[]) p_obj;
        _selectedGameIndex = ((Integer) arrObj[0]).intValue();
        _openGamesDetails = (OpenGamesDetails) arrObj[1];

        
        Command cmd;
        cmd = new Command("Next", Command.OK, 0);
        this.addCommand(cmd);

        cmd = new Command("Back", Command.BACK, 0);
        this.addCommand(cmd);
        this.setCommandListener(this);        
        
    }

    public void commandAction(Command c, Displayable d)
    {
        int cmdType = c.getCommandType();
        if (cmdType == Command.OK)
        {
            Participate_Clicked();
        }
        else if (cmdType == Command.BACK)
        {
            back_Clicked();
        }
    }

    private void back_Clicked()
    {
        AppManager.goToScreen(Screens.NewGame1, _openGamesDetails);
    }

    private void Participate_Clicked()
    {
        if (_nt.getSelectedNumbersAmount() != 6)
        {
            // data inccorect...
            AppManager.setAlert(
                    "Participate in game",
                    "You must select 6 numbers!",
                    AlertType.INFO,
                    "Continue", null,
                    new IAlertCommandListener()
                    {

                        public boolean onOK()
                        {
                            // return to current screen
                            return true;
                        }

                        public boolean onCancel()
                        {
                            // return to current screen
                            return true;
                        }
                    });
            return;
        }

        final StringBuffer sbNumbers = new StringBuffer();
        int[] arr = _nt.getSelectedNumbers();
        for (int i = 0; i < arr.length; i++)
        {
            if (i != 0)
            {
                sbNumbers.append("_");
            }
            sbNumbers.append(String.valueOf(arr[i]));
        }

        AppManager.runBackgroundOperation(
                "Participate in game",
                "Sending game participation information...",
                new IBackgroundOperation()
                {

                    public boolean execute()
                    {
                        // Create request xml
                        String requestUrl =
                                Consts.Urls.Participate +
                                "&userid=" + AppManager.Username +
                                "&pass=" + AppManager.Password +
                                "&gameid=" + _openGamesDetails.GamesIDs[_selectedGameIndex] +
                                "&participation_info=" + sbNumbers.toString();
                        String recvXml = UtilsConnection.GetData(requestUrl);

                        // for testing...................
                        //recvXml = "<root><status>0</status><status_desc>error</status_desc></root>";
                        //recvXml = "<root><status>1</status><status_desc>OK</status_desc><trns_id>AH371JKS</trns_id></root>";

                        ParticipateInGameDetails gameDetails = XmlManager.ProcessXml_ParticipateInGame(recvXml);

                        if (gameDetails.dataCorrect() == true)
                        {
                            // data correct...
                            AppManager.setAlert(
                                    "Participate in game",
                                    "Participation accepted!\r\nTransactio ID: " + gameDetails.TransactionId,
                                    AlertType.INFO,
                                    "Continue", null,
                                    new IAlertCommandListener()
                                    {

                                        public boolean onOK()
                                        {
                                            AppManager.goToScreen(Screens.MainMenu, null);

                                            // manualy switch screens
                                            return false;
                                        }

                                        public boolean onCancel()
                                        {
                                            // not getting here (not cancel button)
                                            return true;
                                        }
                                    });


                            // manualy switching screen, so don't need to return to previos screen
                            return false;
                        }
                        else
                        {
                            // data inccorect...
                            AppManager.setAlert(
                                    "Participate in game", gameDetails.StatusDesc, AlertType.INFO, "Continue", null,
                                    new IAlertCommandListener()
                                    {

                                        public boolean onOK()
                                        {
                                            // return to prev screen
                                            return true;
                                        }

                                        public boolean onCancel()
                                        {
                                            // not getting here (not cancel button)
                                            return true;
                                        }
                                    });

                            // manualy switching screen, so don't need to return to previos screen
                            return false;
                        }
                    }
                });
    }
}

